package com.zzl.example.springboottest.util;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Calendar;
import java.util.Date;

/**
 * @author zengLiangZheng
 * @date 2022年11月07日 10:29
 */
public class DataUtil {

    public final static DateFormat FORMAT_YYYYMMDDHHMMSS = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

    public static void main(String[] args) {
        String strTime = "2023-09-26 16:38:30";
        try {
            Date date = FORMAT_YYYYMMDDHHMMSS.parse(strTime);
            System.out.println(lessMinutes(date, 10));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static Date getPreviousDaysOfNow(int days) {
        Calendar now = Calendar.getInstance();
        now.setTimeInMillis(now.getTimeInMillis() - 1000L * 3600 * 24 * days);
        return now.getTime();
    }



    /**
     * 除法-四舍五入一位小数
     * @param cfSameDayMemberSum
     * @param countHospital
     * @return
     */
    public static double calculateAverageCf(long cfSameDayMemberSum, double countHospital) {
        DecimalFormat decimalFormat = new DecimalFormat("#.#");
        double averageCf = 0;
        if(countHospital > 0){
            String format = decimalFormat.format(cfSameDayMemberSum / countHospital);
            System.out.println(format);
            averageCf = Double.parseDouble(format);
        }
        return averageCf;
    }

    public static void calculateDivide(){
        BigDecimal a = new BigDecimal(6);
        BigDecimal b = new BigDecimal(8);
        System.out.println(a.divide(b));
    }

    /**
     * 思路：将有交集的情况列出,若不符合有交集的情况,则无交集
     * 有交集的两种情况
     * 1.第一个时间段的开始时间在第二个时间段的开始时间和结束时间当中
     * 2.第二个时间段的开始时间在第一个时间段的开始时间和结束时间当中
     * 判断两个时间段是否有交集
     *
     * @param leftStartDate  第一个时间段的开始时间
     * @param leftEndDate    第一个时间段的结束时间
     * @param rightStartDate 第二个时间段的开始时间
     * @param rightEndDate   第二个时间段的结束时间
     * @return 若有交集, 返回true, 否则返回false
     */
    public static boolean hasOverlap(Date leftStartDate, Date leftEndDate, Date rightStartDate, Date rightEndDate) {

        return ((leftStartDate.getTime() >= rightStartDate.getTime())
                && leftStartDate.getTime() < rightEndDate.getTime())
                ||
                ((leftStartDate.getTime() > rightStartDate.getTime())
                        && leftStartDate.getTime() <= rightEndDate.getTime())
                ||
                ((rightStartDate.getTime() >= leftStartDate.getTime())
                        && rightStartDate.getTime() < leftEndDate.getTime())
                ||
                ((rightStartDate.getTime() > leftStartDate.getTime())
                        && rightStartDate.getTime() <= leftEndDate.getTime());

    }

    /**
     * 测试
     * 第一个时间段 2021-12-20 12:00:00 -- 2021-12-22 12:00:00
     * 第二个时间段 2021-12-21 12:00:00 -- 2021-12-23 12:00:00
     * <p>
     * 两者含有交集,则输出true
     *
     * @param args
     */
    public static void hasLap(String[] args) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String leftStart = "2021-12-23 13:00:00";
        String leftEnd = "2021-12-24 11:59:59";
        String rightStart = "2021-12-20 12:00:00";
        String rightEnd = "2021-12-23 12:00:00";
        try {
            Date leftStartDate = simpleDateFormat.parse(leftStart);
            Date leftEndDate = simpleDateFormat.parse(leftEnd);
            Date rightStartDate = simpleDateFormat.parse(rightStart);
            Date rightEndDate = simpleDateFormat.parse(rightEnd);
            // 判断是否有交集
            boolean b = hasOverlap(leftStartDate, leftEndDate, rightStartDate, rightEndDate);
            System.out.println("是否有交集 = " + b);
        } catch (ParseException e) {
            throw new RuntimeException("时间类型转换失败");
        }
    }

    public static int getIntAdd(Integer first, Integer second) {
        if(null == first && null == second){
            return 0;
        }

        if(null == first){
            return second;
        }

        if(null == second){
            return first;
        }
        return first + second;
    }

    /**
     * Duration.between(paymentTime, now)会返回两个时间点之间的持续时间，
     * 然后使用toMinutes()方法将这个持续时间转换为分钟。
     * 最后，如果这个分钟数小于或等于 limitedMinutes，则说明时间未超过 limitedMinutes 分钟
     */
    public static boolean isWithinMinutes(Date dateTime, int limitedMinutes) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dateTime.toInstant(), ZoneId.systemDefault());

        Duration duration = Duration.between(localDateTime, now);
        long minutesBetween = duration.toMinutes();
        System.out.println(minutesBetween);

        return minutesBetween < limitedMinutes;
    }

    /**
     * now less than miniMinutes
     * @param dateTime
     * @param miniMinutes
     * @return
     */
    public static boolean lessMinutes(Date dateTime, int miniMinutes) {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(dateTime.toInstant(), ZoneId.systemDefault());

        Duration duration = Duration.between(localDateTime, now);
        long minutesBetween = duration.toMinutes();
        System.out.println("DateUtil8 lessMinutes minutesBetween:{} 分钟" + minutesBetween);

        return minutesBetween < miniMinutes || minutesBetween == miniMinutes;
    }
}

    
